package com.bawei.es.controller;

import com.bawei.common.core.domain.R;
import com.bawei.es.service.EsIndexService;
import com.bawei.system.domain.CreateEsinfo;
import com.bawei.system.domain.EsMallProductInfo;
import com.bawei.system.domain.SearchResult;
import com.bawei.system.domain.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/esIndex")
@Api(value = "ES索引管理",tags = "ES索引管理")
public class EsIndexController {
    @Autowired
    private EsIndexService esIndexService;

    /**
     * 根据索引名称创建索引
     * @return
     */
    @GetMapping("/create")
    @ApiOperation(value = "根据索引名称创建索引")
    public R<Boolean> indexCreate(String indexName) {
        return esIndexService.indexCreate(indexName);
    }

    /**
     * 根据索引名称、映射结构创建索引
     * @return
     */
    @GetMapping("/createWithMapping")
    @ApiOperation(value = "根据索引名称、映射结构创建索引")
    public R<Boolean> indexCreateWithMapping(@RequestBody CreateEsinfo createEsinfo) {
        return esIndexService.indexCreateWithMapping(createEsinfo);
    }

    /**
     * 根据索引名称查询索引是否存在
     *
     * @return
     */
    @GetMapping("/indexExit")
    @ApiOperation(value = "根据索引名称查询索引是否存在")
    public R<Boolean> indexExit(String indexName) {

        return esIndexService.indexExit(indexName);
    }

    /**
     * 根据索引名称删除索引
     * @return
     */
    @GetMapping("/indexDelete")
    @ApiOperation(value = "根据索引名称删除索引")
    public R<Boolean> indexDelete(String indexName) {
        return esIndexService.indexDelete(indexName);
    }

    /**
     * 根据索引名称获取mapping
     * @return
     */
    @GetMapping("/indexGetMapping")
    @ApiOperation(value = "根据索引名称获取mappings和setting")
    public R<Map<String,Object>> indexGetMapping(String indexName) {
        return esIndexService.indexGtMapping(indexName);
    }



    /**
     * 根据索引名称修改比如增加一个字段、删除一个字段
     * @return
     */
    @PostMapping("/indexUpdate")
    @ApiOperation(value = "根据索引名称修改")
    public R<Boolean> indexUpdate(String indexName, EsMallProductInfo esMallProductInfo) {
        return esIndexService.indexUpdate(indexName,esMallProductInfo);
    }


    /**
     * 查询所有的索引
     */
    @GetMapping("/getAllIndex")
    public R<List<String>> getAllIndex(){
        List<String> list=esIndexService.getAllIndex();
        return R.ok(list);
    }




}
